﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Text;

namespace WooCoo
{
    /// <summary>
    /// 多语言的本地化类
    /// </summary>
    public class Localizer
    {
        private static Localizer _activeCore = CreateDefaultLocalizer();

        public static Localizer Active
        {
            get { return _activeCore; }
            set
            {
                if (value == null)
                    _activeCore = CreateDefaultLocalizer();
                else
                    _activeCore = value;
            }
        }

        public static Localizer CreateDefaultLocalizer()
        {
            return new ResLocalizer();
        }

        public virtual string Language { get { return "English"; } }

        public virtual string GetLocalizeString(StringID name)
        {
            return name.ToString();
        }

        public virtual string GetLocalizeString(StringID name, params string[] para)
        {
            return name.ToString();
        }
    }
    /// <summary>
    /// WooCoo的多语言实现类
    /// </summary>
    public class ResLocalizer : Localizer
    {
        ResourceManager _manager = null;
        public ResLocalizer()
        {
            CreateResourceManager();
        }

        protected virtual void CreateResourceManager()
        {
            if (_manager != null) this._manager.ReleaseAllResources();
            this._manager = new ResourceManager("WooCoo.LocalizationRes", typeof(ResLocalizer).Assembly);
        }

        protected virtual ResourceManager Manager { get { return this._manager; } }
        public override string Language
        {
            get
            {
                return CultureInfo.CurrentUICulture.Name;
            }
        }

        public override string GetLocalizeString(StringID name)
        {
            string ret = Manager.GetString("StringID." + name.ToString());
            if (ret == null) ret = name.ToString();
            return ret;
        }

        public override string GetLocalizeString(StringID name, params string[] para)
        {
            string ret = Manager.GetString("StringID." + name.ToString());
            if (ret == null) ret = name.ToString();
            return string.Format(ret, para);
        }
    }

    public enum StringID
    {
        CanNotReadPermission,
        CanNotInsertAuthorization,
        CanNotDeleteAuthorization,
        CanNotSetNullOrEmpty,
        CanNotEditAuthorization,
        CantCastToUnsupportedPrimitive,
        CantDeserialize,
        ConstructorsWithParametersNotSupported,
        CanOnlyApplyOneKeyPredicate,
        CantTranslateExpression,
        CantReferToPublicField,
        CantConvert,
        CantFoundModule,
        CantAddSameFriendlyName,
        DataPortalProxyNotSpecified,
        DeleteCmdCriteriaNotSpecified,
        DefaultConstructorsNotImplement,
        DataProvideNotSupported,
        ExpressionNotSupportedInProjection,
        LinqOpNoSupported,
        MissingOperationParameterName,
        NotImplementIPermissionProvider,
        NotSupportCommandBuilder,
        NoSaveInvalidException,
        NoSaveChildException,
        NoFoundChangeData,
        NoFoundSvrType,
        NoImpIRepository,
        PermissionProviderNameNotSpecified,
        PropertyNotRegistered,
        RunLocationNotSpecified,
        TableNameNotSpecified,
        TranslationError,
        UnsupportedExpression,
        QueryOptionOutOfOrder,
        QueryOptionsOnlyAllowedOnLeafNodes,
        QueryOptionsOnlyAllowedOnSingletons
    }
}
